package yh.plugins.workflow;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.beaut.project.util.FlowUtility;

public abstract class BasePlugin {
	public static final int init=0;
	public  static final int inFlow=1;
	public  static final int noAgreed=2;
	public  static final int agreed=3;
	
	//-----------------------------

	public  static final int stepOne=1;
	public  static final int afterOne=2;
	public  static final int endFlow=3;
	
	/*businessTableName 业务表名
	 * businessTableStatus 业务更新字段
	 * 
	 * businessFlowBusinessStatus: business_flow表 business_status字段
	 */
public boolean exeCuteUpdate(String businessTableName,String fieldStatusName,int businessTableStatus,int businessFlowBusinessStatus,Connection conn,String runIdStr,int flowId) throws SQLException{
	 if(fieldStatusName==null){
		 fieldStatusName="status";
	 }
     Statement stmt= conn.createStatement();
     String updateSql="update business_flow a  left join "+businessTableName+" b  on  a.business_id = b.id and a.flow_run_id="+runIdStr+" and business_module='"+FlowUtility.getFlowNameById(flowId)+"'" 
     		 +" set a.business_status="+businessFlowBusinessStatus+",b."+fieldStatusName+"="+businessTableStatus;
     stmt.execute(updateSql);
     stmt.close();
     stmt=null;
     
	return true;
}
public String getBusinessId(Connection conn,String runIdStr,int flowId) throws SQLException{
	 Statement stmt= conn.createStatement();
	 String sql="select business_id from business_flow where flow_run_id="+runIdStr+" and business_module='"+FlowUtility.getFlowNameById(flowId)+"'";
	 ResultSet rs=stmt.executeQuery(sql);
	 String businessId=null;
	 if(rs.next()){
		 businessId= rs.getString(1);
	 }
	 rs.close();
	 rs=null;
	 stmt.close();
	 stmt=null;
	 return businessId;
}
public boolean isAgree(String agreeStr){
    if(agreeStr!=null && (agreeStr.equals("同意") || agreeStr.equals("审批通过") || agreeStr.equals("通过"))){
    	return true;
    }
    return false;
}
public int updateBusinessStatus(String tableName,String fieldStatusName,String prcsChoose,Connection conn,String runIdStr,int flowId) throws SQLException{
	int updateStatus=0;
	if(prcsChoose.equals("1,")){
   	  //回退第一步
		updateStatus=noAgreed;
		this.exeCuteUpdate(tableName, fieldStatusName, noAgreed, stepOne, conn, runIdStr, flowId);  
     }
     else if(prcsChoose.equals("0,")){
   	  //走结束，即默认为同意
    	updateStatus=agreed;
    	 this.exeCuteUpdate(tableName, fieldStatusName, agreed, endFlow, conn, runIdStr, flowId);
     }
     else
     {
   	  //退回其他步骤
    	 updateStatus=inFlow;
    	 this.exeCuteUpdate(tableName, fieldStatusName, inFlow, afterOne, conn, runIdStr, flowId);
   	 
     }
	return updateStatus;
	
}
}
